package com.dragon.english_practice_back.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dragon.english_practice_back.entity.Words;
import com.dragon.english_practice_back.service.WordsService;
import com.dragon.english_practice_back.mapper.WordsMapper;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
* @author 伫倚
* @description 针对表【words(单词表)】的数据库操作Service实现
* @createDate 2025-04-13 14:47:29
*/
@Service
public class WordsServiceImpl extends ServiceImpl<WordsMapper, Words>
    implements WordsService{

    @Override
    public Page<Map<String, Object>> getWordsWithProficiency(Page<Map<String, Object>> page, Integer wordBookId) {
        Page<Map<String, Object>> wordsPage = new Page<>(page.getCurrent(), page.getSize());
        wordsPage.setOrders(page.orders());
        // 查询单词书内的单词数目
        long wordsCount = this.count(new QueryWrapper<Words>()
                .eq("book_id", wordBookId));

        wordsPage.setTotal(wordsCount);
        wordsPage.setPages(wordsCount / page.getSize() + 1);

        String OrderBy = "id";
        String Order = "asc";
        if (wordsPage.orders().size() > 0){
            OrderBy = wordsPage.orders().get(0).getColumn();
            Order = wordsPage.orders().get(0).isAsc() ? "asc" : "desc";
        }

        List<Map<String, Object>> wordsList = this.getBaseMapper().getWordsWithProficiency(
                (wordsPage.getCurrent()-1)*wordsPage.getSize(),
                wordsPage.getSize(),
                OrderBy,
                Order,
                wordBookId);

        wordsPage.setRecords(wordsList);
        return wordsPage;
    }
}




